import re

fp1 = open("xaa.log")
fp2 = open("output", "w")
fp3 = open("excel", "w")
pattern1 = r"2020-10-21"
pattern2 = r"lotus"
pattern3 = r"Default"
pattern4 = r"lotus-bench-ori  "
pattern5 = r"Mem:"
pattern6 = r"Swap:"
pattern7 = r"125G"
pattern8 = r"2020-08-04"
nextLine = '\n'
o = "data       time     cpu   mem%  mem    gpu1  gpu2  gpu3  gpu4 gpuMem1 gpuMem2 gpuMem3 gpuMem4"
for line in fp1.readlines():

    m1 = re.search(pattern1, line)  # 时间戳
    m2 = re.search(pattern2, line)  # bench程序
    m3 = re.search(pattern3, line)  # 显卡
    m4 = re.search(pattern4, line)  # 显卡运行程序
    m5 = re.search(pattern5, line)  # Mem
    m6 = re.search(pattern6, line)  # Swap
    m7 = re.search(pattern7, line)  # swap
    m8 = re.search(pattern8, line)  # swap
    if m1 or m2 or m3 or m4 or m5 or m6:
        fp2.write(line)

    if m1 or m8:
        fp3.write(o + "\n")
        time = line[0:20].replace("\n", " ")
        o = time

#    if m2 and (not m4):
#        cpu = line[48:58]
#        o += cpu

    if m5 and m7:
        memo = line[27:32]
        o += (" " + memo)


#    if m6 and m7:
#        swapMem = line[27:32]
#        o += (" " + swapMem)

    if m3:
        gpu = line[60:65]
        o += (" " + gpu)

    if m4:
        gpuMem = line[70:78]
        o += gpuMem


fp1.close()
fp2.close()
fp3.close()
